//
//  GHPartnerViewController.m
//  GodHorses
//
//  Created by Mac on 2017/11/17.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "GHPartnerViewController.h"
#import "GHPartnerCollectionViewCell.h"
#import "GHParterModel.h"
#import "GHPartnerDetailViewController.h"
@interface GHPartnerViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property(nonatomic, strong) UICollectionView                      *collectionView;
@property(nonatomic, strong) NSMutableArray  <GHParterModel*>                   *dataSouce;
@property(nonatomic, assign) NSInteger                     page;
@end

@implementation GHPartnerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"合作商家";
    [self configSubView];
    self.page = 1;
    self.dataSouce = [NSMutableArray new];
}
#pragma mark --subView
- (void)configSubView{
    UICollectionViewFlowLayout * flow = [UICollectionViewFlowLayout new];
    flow.sectionInset = UIEdgeInsetsMake(0, 10, 0, 10);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    flow.scrollDirection = UICollectionViewScrollDirectionVertical;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight - 64 - 44) collectionViewLayout:flow];
    self.collectionView.backgroundColor = GH_backgroundColor;
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[GHPartnerCollectionViewCell class] forCellWithReuseIdentifier:@"item"];
    [self.view addSubview:self.collectionView];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self sendParterRequestwithPage:self.page];
    }];
    [self.collectionView.mj_header beginRefreshing];
}
#pragma --mark UICollectionViewDataSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSouce.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat cellHeight = [GHPartnerCollectionViewCell cellHeight];
    return CGSizeMake(UIScreenWidth/2 - 20, cellHeight);
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    GHPartnerCollectionViewCell * item =[collectionView dequeueReusableCellWithReuseIdentifier:@"item" forIndexPath:indexPath];
    item.model = self.dataSouce[indexPath.row];
    return item;
}
#pragma --mark UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    GHParterModel * model = self.dataSouce[indexPath.row];
    GHPartnerDetailViewController * vc = [GHPartnerDetailViewController new];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [self.collectionView deselectItemAtIndexPath:indexPath animated:NO];
}
#pragma mark --Private Method
- (void)reloadData{
    if (self.dataSouce.count > 5) {
        self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            self.page  ++ ;
            [self sendParterRequestwithPage:self.page];
        }];
    }else{
        self.collectionView.mj_footer = nil;
    }
    [self.collectionView reloadData];
}
#pragma mark --NetWork Method
- (void)sendParterRequestwithPage:(NSInteger)page{
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([GHUserManger defult].userID.length) {
        [dic setObject:[GHUserManger defult].userID forKey:@"uid"];
    }
    if ([GHUserManger defult].token.length) {
        [dic setObject:[GHUserManger defult].token forKey:@"token"];
    }
    
    [dic setObject:@(page) forKey:@"page"];
    [[STNetWrokManger defaultClient] requestWithPath:@"http://test.smqyp.com/Api/index/cooperate"
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 if (page == 1) {
                                                     self.dataSouce = [NSMutableArray new];
                                                 }
                                                 NSDictionary * dataDic = responseObject[@"data"];
                                                 if ([dataDic.allKeys containsObject:@"shop_list"]) {
                                                     NSArray * array = dataDic[@"shop_list"];
                                                     for (NSDictionary * shop in array) {
                                                         GHParterModel * model = [GHParterModel mj_objectWithKeyValues:shop];
                                                         [self.dataSouce addObject:model];
                                                     }
                                                     if (array.count == 0) {
                                                         [self.collectionView.mj_footer endRefreshingWithNoMoreData];
                                                         self.page = 1;
                                                     }
                                                 }

                                                 [self.collectionView.mj_header endRefreshing];
                                                 [self.collectionView.mj_footer endRefreshing];
                                                 [self reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.collectionView.mj_header endRefreshing];
                                                 [self.collectionView.mj_footer endRefreshing];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}

@end
